package cn.com.cjwsjy.monitor.platform.earlywarn.controller;

import cn.com.cjwsjy.monitor.platform.earlywarn.requestVO.EarlyWarningPlanQuery;
import cn.com.cjwsjy.monitor.platform.earlywarn.service.EarlyWarningPlanService;
import cn.com.cjwsjy.monitor.platform.earlywarn.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Map;

/**
 * 应急预案管理相关接口
 *
 * @author
 */
@RestController
@RequestMapping("/earlywarn/earlywarningplan")
public class EarlyWarningPlanController {
    @Autowired
    private EarlyWarningPlanService earlyWarningPlanService;


    /**
     * 获取所有工程部位
     * @return
     */
    @GetMapping("/getAllPositions")
    public R getAllPositions() {
        return earlyWarningPlanService.getAllPositions();
    }

    /**
     * 获取所有预警等级
     * @return
     */
    @GetMapping("/getAllLevels")
    public R getAllLevels() {
        return earlyWarningPlanService.getAllLevels();
    }

    /**
     * 添加应急预案
     * @return
     */
    @PostMapping("/addPlan")
    public R addPlan(@RequestParam("docName") String docName, @RequestParam("warningLevel") String warningLevel,
                     @RequestParam("applicablePeriod") String applicablePeriod, @RequestParam("positionName") String positionName,
                     @RequestParam("file") MultipartFile file,@RequestParam("uploadPerson") String uploadPerson){
        try {
            return earlyWarningPlanService.addPlan(docName,warningLevel,applicablePeriod,positionName,file,uploadPerson);
        } catch (Exception e) {
            if(e.getCause() instanceof SQLIntegrityConstraintViolationException){
                return R.error("系统中不存在此工程部位！");
            }
            e.printStackTrace();
        }
        return R.error("未知错误,请联系管理员");
    }
    /**
     * 获取所有可适用时期
     * @return 可适用时期列表
     */
    @GetMapping("/getAllPeriods")
    public R getAllPeriods(){
        return  earlyWarningPlanService.getAllPeriods();
    }
    /**
     * 修改应急预案
     * @return
     */
    @PostMapping("/updatePlan")
    public R updatePlan(@RequestParam("id") Long id, @RequestParam("docName") String docName, @RequestParam("warningLevel") String warningLevel,
                        @RequestParam("applicablePeriod") String applicablePeriod, @RequestParam("positionName") String positionName,
                        @RequestParam(value = "file", required = false) MultipartFile file,@RequestParam("uploadPerson") String uploadPerson){
        try {
            return earlyWarningPlanService.updatePlan(id,docName,warningLevel,applicablePeriod,positionName,file,uploadPerson);
        } catch (Exception e) {
            if(e.getCause() instanceof SQLIntegrityConstraintViolationException){
                return R.error("系统中不存在此工程部位！");
            }
            e.printStackTrace();
        }
        return R.error("未知错误,请联系管理员");
    }
    /**
     * 删除应急预案
     * @return
     */
    @GetMapping("/deletePlan/{id}")
    public R deletePlan(@PathVariable Long id){
        return  earlyWarningPlanService.deletePlan(id);
    }
    
    /**
     * 条件查询应急预案
     * @param earlyWarningPlanQuery 查询条件
     * @return
     */
    @PostMapping("/queryPlan")
    public R queryPlan(@RequestBody EarlyWarningPlanQuery earlyWarningPlanQuery){
        return earlyWarningPlanService.queryPlan(earlyWarningPlanQuery);
    }

    /**
     * 通过预警消息设置表id查询应急预案信息
     * @param id 预警消息设置表id
     * @return
     */
    @GetMapping ("/getPlanInfoByWarningMessageId/{id}")
    public R getPlanInfoByWarningMessageId(@PathVariable int id){
        return earlyWarningPlanService.getPlanInfoByWarningMessageId(id);
    }

    /**
     * 下载应急预案
     * @param id 应急预案对应的id
     * @param response 响应体
     * @return
     */
    @GetMapping("/download/{id}")
    public R downloadPlan(@PathVariable Long id, HttpServletResponse response){
        return earlyWarningPlanService.downloadPlan(id, response);
    }

}
